/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buisness;

import dal.FriendsDAL;
import dal.MessengersDAL;
import dal.UsersDAL;
import dal.util.MessageComparator;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.LineFriends;
import models.Messenger;
import models.Users;

/**
 *
 * @author gabrielle
 */
public class Messages extends HttpServlet {

    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        //afficher les messages
        String username = (String)request.getSession().getAttribute("username");
        String type=(String)request.getSession().getAttribute("type");
        if(type!=null && type.equalsIgnoreCase("news"))
        {
            Users user=UsersDAL.getUserByUsername(username);
            ArrayList<LineFriends> friends = FriendsDAL.getAllFriendsByUser(user);
            ArrayList<Messenger> allMessenger=new ArrayList();
            allMessenger.addAll(MessengersDAL.getMessengersByUsername(username));
            for (LineFriends friend : friends) 
            {
                allMessenger.addAll(MessengersDAL.getMessengersByUsername(friend.getUser().getUsername(),new Date(System.currentTimeMillis()),friend.getAddDate() ));
            }
            Collections.sort(allMessenger, new MessageComparator());
            request.getSession().setAttribute("Messengers", allMessenger);
            response.sendRedirect("./news.jsp");
            
        }
        else if(type!=null && type.equalsIgnoreCase("wall"))
        {
            ArrayList<Messenger> allMessenger=new ArrayList();
            allMessenger.addAll(MessengersDAL.getMessengersByUsername(username));
            Collections.sort(allMessenger, new MessageComparator());
            request.getSession().setAttribute("Messengers", allMessenger);
            response.sendRedirect("./wall.jsp");
        }
                
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
       //ajouter un nouveau message
       String username=(String) request.getSession().getAttribute("username");
       Users user = UsersDAL.getUserByUsername(username);
       String body = request.getParameter("body");

       //message public car wall et posté par user et donc non system
       Messenger mail =new Messenger(user, 0, body, 0, 0, true, false, new Date(System.currentTimeMillis()));
       MessengersDAL.insertMessenger(mail);
       response.sendRedirect("./messages");
    }

   
}
